﻿using Ext.Net;
using NOAH.CMS.Repository;

using System;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;

public partial class admin_Modules_NewsEvent_Images_AddOrEditImageCategory : System.Web.UI.Page
{
    private string id = string.Empty;


    protected void Page_Load(object sender, EventArgs e)
    {
        id = base.Request.QueryString["id"] ?? "0";

        if (!ExtNet.IsAjaxRequest)
        {
            this.ShowDetail();
        }
    }

    public void SaveImageCategories(object sender, DirectEventArgs e)
    {
        bool isAdd = false;
        if (this.id == "0")
        {
            isAdd = true;
        }
        try
        {
            int temp = 0;
            ImageCategory newImageCategory = null;
            ImageCategoryRepository imageCategoryBiz = new ImageCategoryRepository();
            if (isAdd)
            {
                newImageCategory = new ImageCategory
                {
                    CreatedOn = new DateTime?(DateTime.Now),
                    CreatedBy = BasePage.GetCurrentUserName()
                };
            }
            else
            {
                newImageCategory = imageCategoryBiz.Get(int.Parse(this.id));
            }
            newImageCategory.EditedBy = BasePage.GetCurrentUserName();
            newImageCategory.EditedOn = new DateTime?(DateTime.Now);
            newImageCategory.Name = this.txtTitle.Text;
            newImageCategory.Description = this.txtDescription.Text;
            int.TryParse(this.txtOrder.Text, out temp);
            newImageCategory.Order = new short?((short)temp);
            newImageCategory.Url = this.triggerImage.Text;
            if (isAdd)
            {
                imageCategoryBiz.Add(newImageCategory);
            }
            imageCategoryBiz.SubmitChanges();
            BasePage.RegisterCallBackFromChild(this.SM, string.Empty);
            Util.ShowMessage(Constants.UPDATE_SUCCESS);
            e.Success = true;
        }
        catch
        {
            e.Success = false;
            Util.ShowMessage2(Constants.UPDATE_FAILURE);
        }
    }

    private void ShowDetail()
    {
        this.txtTitle.Focus(false, 100);
        if (this.id != "0")
        {
            this.wdAddOrEditImageCategory.Show();
            ImageCategory editImageCategory = new ImageCategoryRepository().Get(int.Parse(this.id));
            this.txtTitle.Text = editImageCategory.Name;
            this.txtDescription.Text = editImageCategory.Description;
            this.txtOrder.Text = editImageCategory.Order.ToString();
            this.triggerImage.Text = editImageCategory.Url;
        }
    }
}